import json
import requests
import re

import execjs


def get_sign(text="happy new year my baby"):
    url = "https://fanyi.baidu.com"

    # 整理请求头
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
        "cookie": "BAIDUID=F991FA0E1514AF1AF94E3C3839B9EA87:FG=1; BDUSS=GRBT1o4clBwVWt5RGdCRWRVSTNWeUViM2JPSTFJd1g0bktJaHVwa21rZzU5dGxmSUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADlpsl85abJfW; BDUSS_BFESS=GRBT1o4clBwVWt5RGdCRWRVSTNWeUViM2JPSTFJd1g0bktJaHVwa21rZzU5dGxmSUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADlpsl85abJfW; BIDUPSID=F991FA0E1514AF1AF94E3C3839B9EA87; PSTM=1605528303; H_PS_PSSID=1457_32856_33059_33099_33101_32962_32937_22160; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=5; BAIDUID_BFESS=F991FA0E1514AF1AF94E3C3839B9EA87:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1605534134,1605572143; Hmery-Time=3422352928; BA_HECTOR=8585ak8h85218lbhft1fr6aon0o; __yjsv5_shitong=1.0_7_3f591980259ef99165c848b2437045f16e7f_300_1605578261386_49.89.253.124_554bc87f; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1605578261; yjs_js_security_passport=5ef55a789beef4c7157c9ddf217ff7c73838ea5d_1605578269_js"
    }

    # 获取请求
    r = requests.get(url, headers=headers)
    sign_i = re.findall(";window.gtk = ('.*?');", r.content.decode("utf-8"))[0]
    print(sign_i)  # '320305.131321201'

    with open("百度翻译.js", "r", encoding="utf-8") as f:
        js_content = f.read()

    # js中添加一行gtk
    js_content = js_content.replace('"320305.131321201"', sign_i)
    # print(js_content)

    # 执行js
    context = execjs.compile(js_content)
    sign = context.call("e", text)
    print(sign)
    return sign


def baidu_translate(text, sign):
    url = "https://fanyi.baidu.com/v2transapi?from=en&to=zh"
    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1",
        "cookie": "BAIDUID=F991FA0E1514AF1AF94E3C3839B9EA87:FG=1; BDUSS=GRBT1o4clBwVWt5RGdCRWRVSTNWeUViM2JPSTFJd1g0bktJaHVwa21rZzU5dGxmSUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADlpsl85abJfW; BDUSS_BFESS=GRBT1o4clBwVWt5RGdCRWRVSTNWeUViM2JPSTFJd1g0bktJaHVwa21rZzU5dGxmSUFBQUFBJCQAAAAAAAAAAAEAAAAn4NUTZG9uZ7fJx-8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADlpsl85abJfW; BIDUPSID=F991FA0E1514AF1AF94E3C3839B9EA87; PSTM=1605528303; H_PS_PSSID=1457_32856_33059_33099_33101_32962_32937_22160; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=5; BAIDUID_BFESS=F991FA0E1514AF1AF94E3C3839B9EA87:FG=1; BA_HECTOR=ah2l2k81ag81a42eiv1fr64o30o; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1605534134,1605572143; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1605572143; __yjsv5_shitong=1.0_7_3f591980259ef99165c848b2437045f16e7f_300_1605572143456_49.89.253.124_8be752b4; yjs_js_security_passport=2f8833f001ff7417f9f0ef56647126dba19d98fe_1605572144_js"
    }
    data = {
        "query": text,
        "from": "en",
        "to": "zh",
        "token": "871d29656fa8b9c6fdf4529446b39dfa",
        "sign": sign
    }

    response = requests.post(url=url, data=data, headers=headers)

    # print("响应\n\n", response.content.decode())

    ret = json.loads(response.content.decode())
    translate_ret = ret["trans_result"]["data"][0]["dst"]
    print(translate_ret)
    return translate_ret


if __name__ == '__main__':
    # 1. 输入要翻译的英文
    text = input("请输入要翻译的英文:")  # 假如要翻译的英文是：happy new year my baby
    # 2. 计算出sign
    sign = get_sign(text)
    # 3. 发送翻译请求
    ret = baidu_translate(text, sign)
    print("英文:", text, "翻译为应为：", ret)
